# -*- coding: utf-8 -*-
import redis

active="dev"
redis_config={
    "dev":{
        "host":'localhost',
        "port":6379,
        "db":0,
        "password":""
    }
}
redis_properties=redis_config[active]
class RedisUtil:
    def __init__(self, host='localhost', port=6379, db=0, password=None):
        self.redis_client = redis.Redis(host=redis_properties['host'], port=redis_properties['port'],
                                   db=redis_properties['db'], password=redis_properties['password'], decode_responses=True)

        # self.redis_client = redis.StrictRedis(host=host, port=port, db=db, password=password, decode_responses=True)

    def set_value(self, key, value, expire=None):
        """
        设置键值对，并可选择设置过期时间
        :param key: 键
        :param value: 值
        :param expire: 过期时间（秒），可选
        """
        if expire:
            self.redis_client.setex(key, expire, value)
        else:
            self.redis_client.set(key, value)

    def get_value(self, key):
        """
        获取键对应的值
        :param key: 键
        :return: 值
        """
        return self.redis_client.get(key)

    def delete_key(self, key):
        """
        删除键
        :param key: 键
        """
        self.redis_client.delete(key)

    def exists_key(self, key):
        """
        检查键是否存在
        :param key: 键
        :return: 存在返回 True，否则返回 False
        """
        return self.redis_client.exists(key)

    def increment_value(self, key, amount=1):
        """
        增加键的值
        :param key: 键
        :param amount: 增加的量
        :return: 增加后的值
        """
        return self.redis_client.incr(key, amount)

    def decrement_value(self, key, amount=1):
        """
        减少键的值
        :param key: 键
        :param amount: 减少的量
        :return: 减少后的值
        """
        return self.redis_client.decr(key, amount)

    def push_to_list(self, key, *values):
        """
        将值推入列表
        :param key: 键
        :param values: 值
        """
        self.redis_client.rpush(key, *values)


# redis_client=RedisUtil()
# redis_client.set_value("123","9999")
# value=redis_client.get_value("123")
# pp=redis_client.increment_value("ddd")
# print(pp)
# pp=redis_client.increment_value("ddd")
# print(pp)
